package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.constant.ProductAuditStatusEnum;
import com.atguigu.spzx.common.constant.ProductStatusEnum;
import com.atguigu.spzx.common.utils.BeanUtils;
import com.atguigu.spzx.manager.mapper.ProductDetailsMapper;
import com.atguigu.spzx.manager.mapper.ProductMapper;
import com.atguigu.spzx.manager.mapper.ProductSkuMapper;
import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.manager.entity.Product;
import com.atguigu.spzx.manager.entity.ProductDetails;
import com.atguigu.spzx.manager.entity.ProductSku;
import com.atguigu.spzx.manager.vo.product.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * ProductServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2023/10/30
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;

    /**
     * 条件分页查询
     * @param pageParam
     * @param reqVo
     * @return
     */
    @Override
    public Page<ProductQueryRespVo> getProductPageByQuery(Page<Product> pageParam, ProductQueryReqVo reqVo) {
        return productMapper.getProductPageByQuery(pageParam,reqVo);
    }

    /**
     * 添加商品
     * @param reqVo
     */
    @Override
    @Transactional
    public void saveProduct(ProductSaveReqVo reqVo) {
        //1.保存商品基本信息 product表
        Product product = BeanUtils.toBean(reqVo, Product.class);
        product.setStatus(ProductStatusEnum.INIT.getCode());
        product.setAuditStatus(ProductAuditStatusEnum.INIT.getCode());
        productMapper.insert(product);

        //2.获取商品的sku列表，保存sku信息 product_sku表
        List<ProductSku> productSkuList = reqVo.getProductSkuList();
        for (int i = 0; i < productSkuList.size(); i++) {
            ProductSku productSku = productSkuList.get(i);
            //商品编号
            productSku.setSkuCode(product.getId() + "_" + i);
            //商品id
            productSku.setProductId(product.getId());
            //sku名称
            productSku.setSkuName(product.getName() + productSku.getSkuSpec());
            //销量
            productSku.setSaleNum(0);
            //线上状态
            productSku.setStatus(ProductStatusEnum.INIT.getCode());
            productSkuMapper.insert(productSku);
        }

        //3.保存商品详情数据 product_details表
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(reqVo.getDetailsImageUrls());
        productDetailsMapper.insert(productDetails);
    }

    /**
     * 根据id查询商品详情
     * @param id
     * @return
     */
    @Override
    public ProductDetailsRespVo getProductById(Long id) {
        //1.根据id查询商品基本信息 product
        ProductDetailsRespVo respVo = productMapper.getProductById(id);

        //2.根据id查询商品sku列表 product_sku
        List<ProductSku> productSkuList = productSkuMapper.getByProductId(id);

        //3.根据id查询商品详情 product_details
        ProductDetails productDetails = productDetailsMapper.getByProductId(id);
        String imageUrls = productDetails.getImageUrls();

        respVo.setProductSkuList(productSkuList);
        respVo.setDetailsImageUrls(imageUrls);
        return respVo;
    }

    /**
     * 修改商品
     * @param reqVo
     */
    @Override
    @Transactional
    public void updateProductById(ProductUpdateReqVo reqVo) {
        //修改product
        Product product = BeanUtils.toBean(reqVo, Product.class);
        productMapper.updateById(product);

        //修改product_sku
        List<ProductSku> productSkuList = reqVo.getProductSkuList();
        for (ProductSku productSku : productSkuList) {
            productSkuMapper.updateById(productSku);
        }

        //修改product_details
        ProductDetails productDetails = productDetailsMapper.getByProductId(product.getId());
        productDetails.setImageUrls(reqVo.getDetailsImageUrls());
        productDetailsMapper.updateById(productDetails);
    }

    /**
     * 根据id删除商品
     * @param id
     */
    @Override
    @Transactional
    public void deleteProductById(Long id) {
        //删除product
        productMapper.deleteById(id);

        //删除product_sku
        productSkuMapper.deleteProductSkuByProductId(id);

        //删除product_details
        productDetailsMapper.deleteProductDetailsByProductId(id);
    }

    /**
     * 商品审核
     * @param id
     * @param auditStatus
     */
    @Override
    public void updateAuditStatusById(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if(ProductAuditStatusEnum.PASS.getCode().equals(auditStatus)) {
            product.setAuditStatus(ProductAuditStatusEnum.PASS.getCode());
            product.setAuditMessage(ProductAuditStatusEnum.PASS.getDesc());
        } else {
            product.setAuditStatus(ProductAuditStatusEnum.UNPASS.getCode());
            product.setAuditMessage(ProductAuditStatusEnum.UNPASS.getDesc());
        }
        productMapper.updateById(product);
    }

    /**
     * 商品上下架
     * @param id
     * @param status
     */
    @Override
    public void updateStatusById(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if(ProductStatusEnum.ON_SHELF.getCode().equals(status)) {
            product.setStatus(ProductStatusEnum.ON_SHELF.getCode());
        } else {
            product.setStatus(ProductStatusEnum.OFF_SHELF.getCode());
        }
        productMapper.updateById(product);
    }
}
